package com.cpp.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cppddd.util.SinaWeiboUtil;

import weibo4j.model.User;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;

/**
 * Servlet implementation class UserInfo
 */
public class UserInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		JSONObject jsonObject = new JSONObject();
		PrintWriter pw = response.getWriter();
		if(request.getSession().getAttribute("user") == null){
			try {
				jsonObject.put("success", "1");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			String uid = request.getParameter("uid");
			String screenName = request.getParameter("screenName");			
			User user = null;
			if(uid != null){
				try {
					user = new SinaWeiboUtil().showUserById(uid);
				} catch (WeiboException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				user = (User)request.getSession().getAttribute("user");
			}
			
			if(screenName != null && !screenName.equals("")){
				try {
					user = new SinaWeiboUtil().showUserByScreenName(screenName);
				} catch (WeiboException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
			try {
				jsonObject.put("success", "0");
				jsonObject.put("screenName", user.getScreenName());//微博名称
				jsonObject.put("location", user.getLocation());//地址
				jsonObject.put("profileImageUrl", user.getProfileImageUrl());//自定义图像
				jsonObject.put("gender", user.getGender());//性别,m--男，f--女,n--未知
				jsonObject.put("followersCount", user.getFollowersCount());//粉丝数
				jsonObject.put("friendsCount", user.getFriendsCount());//关注数
				jsonObject.put("statusesCount", user.getStatusesCount());//微博数
				jsonObject.put("verified", user.isVerified());//是否是认证用户
				jsonObject.put("verifiedType", user.getVerifiedType());//认证类型
				jsonObject.put("verifiedReason", user.getVerifiedReason());//认证原因
				
				pw.write(jsonObject.toString());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		pw.close();
		
	}

}
